import { computed, onMounted, Ref, ref, nextTick, reactive } from 'vue';
import type { ShortTouch, ReqParam, OpButtonKey } from '@/domain/dataBoard/types/shortclass/touch';
import { getTouch, getSelection } from '@/domain/dataBoard/api/shortClass/touch';
import { usePageList, apiType } from '@/domain/common/hooks/pageList';
import type { Column, OpGroupCallBack, ResSearch } from '@/domain/common/types/page';
import { useUserInfoStore } from '@/domain/authority/hooks/login';
import { ChildId, ErrorChildId, PrimaryID } from '@/domain/dataBoard/types/common';
import { Overview } from '@/domain/common/types/common';
import { Tend } from '../../types/tend';

export const useTouchData = (childId: ChildId) => {
  const searchRef: Ref<ResSearch> = ref([]);
  const api: apiType<ShortTouch, ReqParam> = {
    list: getTouch,
    search: searchRef.value
  };
  const userStore = useUserInfoStore();

  const { IDString, rowlist, columnlist, paging, searchParam, searchList, setSizeChange, loadTable, getSelectRowsCallBack, reloadList, getResData } =
    usePageList<PrimaryID, ShortTouch, ReqParam>(api, 'id');
  const overview: Ref<Overview | []> = computed(() => {
    const data = getResData();
    if (data && data.overview) {
      return data.overview;
    } else {
      return [];
    }
  });
  const trendList: Ref<Tend | []> = computed(() => {
    const data = getResData();
    if (data && data.trend) {
      return data.trend;
    } else {
      return [];
    }
  });
  //页面操作回掉
  const opGroupCallBack: OpGroupCallBack<ShortTouch, OpButtonKey> = async (key, node) => {};

  const dataSource = computed(() => rowlist.value.map((node) => Object.assign(node.row, { opGroup: node.op_group })));

  onMounted(async () => {
    const res = await getSelection();
    searchList.value = res.data;
    if (childId === ErrorChildId.DefaultChildId) {
      (searchParam as ReqParam).child_id = childId;
    }
    nextTick(async () => {
      await reloadList();
    });
  });
  return {
    IDString,
    dataSource,
    rowlist,
    paging,
    searchParam,
    columnlist,
    trendList,
    searchList,
    setSizeChange,
    loadTable,
    getSelectRowsCallBack,
    opGroupCallBack,
    reloadList,
    overview
  };
};
